const res=document.getElementById('result');
const his_in=document.getElementById('hisIn');
const Clear=document.getElementById('clear')
const E='2.718281828459045';
const pi='3.141592653589793';
// let stack_dig=new Stack();
// let stack_cal=new Stack();
let stack_del=new Stack();
let stack_his=new Stack();
let stack_equ=new Stack();
let temp;let cnt=0;
let digital=['0','1','2','3','4','5','6','7','8','9','10','e','π','.'];
let cal_symbol=['mod','÷','X','-','+','(',')','x^y','exp'];
let cal_symbol_map=['Mod','/','*','-','+','^','Exp','(',')'];
let cal_symbol_can=['Mod','/','*','-','+','^','Exp','('];
let equations_sin=['n!','x^2','2^x','√x','10^x','log','ln','1/x','+/-','|x|'];
let equations=['1/x','|x|','x^2','2^x','√x','10^x','log','ln','+/-','n!'];
function liveScreen(enteredValue) {
    if(!his_in.value){
        his_in.value='';
    }
    if(enteredValue==='%'){
        enteredValue='mod';
    }
    if(cal_symbol.indexOf(enteredValue)!==-1 || equations.indexOf(enteredValue)!==-1) {
        cal_deal(enteredValue);
    }else if(digital.indexOf(enteredValue)!==-1){
        dig_deal(enteredValue);
    }else if(enteredValue==='='){
        if((res.value==='0' && his_in.value==='') || res.value!=='0'){
            his_in.value+=res.value;
        }
        //res.value='0';
        calculate();
    }

}
Clear.addEventListener('click',clear);
$('#CE').on('click',CE);
$('.del').on('click',function (){
    if(!stack_del.isEmpty()){
        temp=stack_del.peek();
        stack_del.pop();
        if(res.value!=='0'){
            if(res.value.length-temp.length===0){
                res.value="0";
            }else{
                res.value=res.value.substring(0,res.value.length-temp.length);
            }
        }
    }
})
function clear(){
    res.value="0";
    his_in.value="";
    stack_del.clear();
    stack_equ.clear();
    cnt=0;
}
function CE(){
    res.value='0';
    stack_del.clear();
}
function dig_deal(val){
    if(val==='e'){
        res.value=E;
    }else if(val==='π'){
        res.value=pi;
    }else if(val==='.'){
        res.value+=val;
    } else{
        if(res.value===E || res.value===pi || res.value==='0'){
            res.value=val;
        }else{
            res.value+=val;
        }
    }
    stack_del.push(val);
}
function cal_deal(val) {
    if(val==='('){//输入（
        if(res.value==='0'){
            if(his_in.value==='' || cal_symbol_can.indexOf(stack_equ.peek())!==-1){
                his_in.value+='(';
            }else{
                his_in.value+='*'+'(';
                stack_equ.push('*');
            }
        }else{
            temp=dig_push(res.value);
            if(!stack_equ.isEmpty()){
                his_in.value+=temp+'*'+map[val];
                stack_equ.push('*');
                res.value='0';
            }
        }
        stack_equ.push(val);
        cnt++;
    }else if(val===')'){//输入）
        if(cnt>0){
            temp=dig_push(res.value);
            if((temp===0 && (his_in.value==='' || cal_symbol_can.indexOf(stack_equ.peek())!==-1)) || temp!==0){
                his_in.value+=temp+map[val];
                if(temp===0) stack_equ.push(temp);
            }else{
                his_in.value+=map[val];
            }
            res.value='0';
            stack_equ.push(map[val]);
            cnt--;
        }
    }else if(equations.indexOf(val)!==-1){//输入高级运算符
        temp=dig_push(res.value);
        if(!stack_equ.isEmpty()){
            if((temp===0 && his_in.value==='')||temp!==0){
                his_in.value+=temp;
            }
            cal_equ(val);
            res.value='0';
        }
    } else{//输入基础运算符
        temp=dig_push(res.value);
        if((temp===0 && (his_in.value==='' || cal_symbol_can.indexOf(stack_equ.peek())!==-1))||temp!==0){
            if(cal_symbol.indexOf(stack_equ.peek())!==-1){
                stack_equ.push(0);
            }
            his_in.value+=temp+map[val];
            res.value='0';
        }else if(temp===0 && his_in.value!==''){
            his_in.value+=map[val];
            res.value='0';
        }
        stack_equ.push(map[val]);
    }
}
function dig_push(value){
    if(value===E){
        stack_equ.push(E);
        return E;
    }else if(value===pi){
        stack_equ.push(pi);
        return pi;
    }else if(value.indexOf('.')!==-1){
        let num=parseFloat(value);
        if(!isNaN(num)){
            if((num===0 && his_in.value==='')||num!==0){
                stack_equ.push(num);
            }
            return num;
        }else{
            alert('错误:dig_push_float');
        }
    } else {
        let num=parseInt(value);
        if(!isNaN(num)){
            if((num===0 && his_in.value==='')||num!==0){
                stack_equ.push(num);
            }
            return num
        }else{
            alert('错误:dig_push_int');
        }
    }
    return 0;
}

